<?php
/**
 * Created by PhpStorm.
 * User: 式神 (luck48.com)
 * Email: 289650682@qq.com
 * Name: ${NAME}Administrator
 * Date: 2017-04-01
 * Time: 11:30
 */
namespace App\Providers;

use App\Http\Models\Article;
use App\Http\Models\ArticleAttr;
use App\Http\Models\Category;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class IndexServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        $this->bladeDirectives();
    }

    private function bladeDirectives(){

        /*Blade::directive('datetime', function($expression) {
            dd($expression);
            return "<?php echo $expression->format('m/d/Y H:i'); ?>";
        });*/
        Blade::directive('position', function($expression) {
            $matches=preg_split('/as/is',$expression);
            $datamatches=preg_split('/\|/is',trim($matches[0]));


            $ids=isset($datamatches[0])?array_filter(preg_split('/,/is',$datamatches[0])):0;
            $cids=isset($datamatches[1])?array_filter(preg_split('/,/is',$datamatches[1])):0;
            $attr=new ArticleAttr();

            if(count($ids)>0 && $ids!=0){
                $attr=$attr->whereIn('attr_id',$ids);
            }
            $attr=$attr->get();
            $adata=[];
            foreach($attr as $k=>$v){
                $adata[$k]=$v->article_id;
            }

            $article=new Article();
            if(count($cids)>0 && $cids!=0){
                $article=$article->whereIn('cid',$cids);
            }
            if(count($adata)>0){
                $article=$article->whereIn('id',$adata);
            }
            $article=$article->orderBy('updated_at','Desc')->get();

            $iteratee='';
            foreach($article as $k=>$v){
                $iteratee.=$v.'|==|';
            };
            $iteratee = "'".$iteratee."'";
            $iteration = trim($matches[1]);

            $initS="\$__foreachData = array_filter(explode('|==|',{$iteratee}));";
            $initS.="\$__d = [];";
            $initS.="foreach(\$__foreachData as \$_k=>\$_v){";
            $initS.="\$__d[\$_k]=json_decode(\$_v,true);";
            $initS.="};";
            $initLoop = "\$__currentLoopData = \$__d; \$__env->addLoop(\$__currentLoopData);";
            $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';

            return "<?php {$initS} {$initLoop} foreach(\$__currentLoopData as \$k=>{$iteration}): {$iterateLoop} ?>";
        });
        Blade::directive('endposition', function($expression) {
            return '<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
        });

        //栏目列表
        Blade::directive('content', function($expression) {
            $matches=preg_split('/as/is',$expression);
            $matches2=preg_split('/\|/is',$matches[1]);

            $ids=isset($matches[0])?array_filter(preg_split('/,/is',$matches[0])):0;

            $order=isset($matches2[1])?array_filter(preg_split('/,/is',$matches2[1])):'';

            $article=new Article();
            if(count($ids)>0 && $ids!=0){
                $article=$article->whereIn('cid',$ids);
            }
            if(count($order)>1){
                $article=$article->orderBy($order[0],$order[1]);
            }
            $article=$article->get();

            $iteratee='';
            foreach($article as $k=>$v){
                $iteratee.=$v.'|==|';
            };
            $iteratee = "'".$iteratee."'";
            $iteration = trim($matches2[0]);

            $initS="\$__foreachData = array_filter(explode('|==|',{$iteratee}));";
            $initS.="\$__d = [];";
            $initS.="foreach(\$__foreachData as \$_k=>\$_v){";
            $initS.="\$__d[\$_k]=json_decode(\$_v,true);";
            $initS.="};";
            $initLoop = "\$__currentLoopData = \$__d; \$__env->addLoop(\$__currentLoopData);";
            $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';

            return "<?php {$initS} {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} ?>";
        });
        Blade::directive('endcontent', function($expression) {
            return '<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
        });

        //栏目列表
        Blade::directive('menu', function($expression) {
            $matches=preg_split('/as/is',$expression);
            $matches2=preg_split('/\|/is',$matches[1]);

            $ids=isset($matches[0])?trim($matches[0]):0;

            $order=isset($matches2[1])?array_filter(preg_split('/,/is',$matches2[1])):'';

            $cate=new Category();
            if(count($order)>1){
                $cate=$cate->orderBy($order[0],$order[1]);
            }
            $cate=$cate->get();
            $cates=qtree($cate,'pid',$ids);

            $iteratee='';
            foreach($cates as $k=>$v){
                $iteratee.=$v.'|==|';
            };
            $iteratee = "'".$iteratee."'";
            $iteration = trim($matches2[0]);

            $initS="\$__foreachData = array_filter(explode('|==|',{$iteratee}));";
            $initS.="\$__d = [];";
            $initS.="foreach(\$__foreachData as \$_k=>\$_v){";
            $initS.="\$__d[\$_k]=json_decode(\$_v,true);";
            $initS.="};";
            $initLoop = "\$__currentLoopData = \$__d; \$__env->addLoop(\$__currentLoopData);";
            $iterateLoop = '$__env->incrementLoopIndices(); $loop = $__env->getLastLoop();';

            return "<?php {$initS} {$initLoop} foreach(\$__currentLoopData as {$iteration}): {$iterateLoop} ?>";
        });
        Blade::directive('endmenu', function($expression) {
            return '<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>';
        });
    }

}